//
//  OrderScoreStarTableViewCell.m
//  customer
//
//  Created by Holothurian on 2018/11/10.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "OrderScoreStarTableViewCell.h"

@interface OrderScoreStarTableViewCell ()<HSStarViewDelegate>

@end

@implementation OrderScoreStarTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        AutoWeakSelf
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.textColor = [HSPublic colorWithHexString:@"#666666"];
        _nameLabel.font = Font28;
        [self.contentView addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.contentView).offset(10);
            make.left.equalTo(weakSelf.contentView).offset(15);
            make.bottom.equalTo(weakSelf.contentView);
            make.width.mas_offset(70);
        }];
        
        _starView = [[HSStarView alloc]initWithFrame:CGRectMake(105, 15, 100, 20) numberOfStars:5];
        _starView.delegate = self;
        _starView.allowIncompleteStar = YES;
        _starView.hasAnimation = YES;
        _starView.scorePercent = 0;
        [self.contentView addSubview:_starView];
        
        _scoredLabel = [[UILabel alloc]init];
        _scoredLabel.textColor = [HSPublic colorWithHexString:@"666666"];
        _scoredLabel.font = Font28;
        _scoredLabel.text = @"0.0分";
        [self.contentView addSubview:_scoredLabel];
        [_scoredLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.starView.mas_right).offset(10);
            make.top.equalTo(weakSelf.contentView).offset(10);
            make.width.mas_offset(50);
            make.bottom.mas_offset(0);
        }];
    }
    return self;
}

- (void)cellForRemarkFrame{
    AutoWeakSelf
    [_nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(0);
        make.left.equalTo(weakSelf.contentView).offset(15);
        make.bottom.equalTo(weakSelf.contentView).offset(-10);
        make.width.mas_offset(70);
    }];
    if (self.starView) {
        [self.starView removeFromSuperview];
    }
    _starView = [[HSStarView alloc]initWithFrame:CGRectMake(105, 5, 100, 20) numberOfStars:5];
    _starView.delegate = self;
    _starView.allowIncompleteStar = YES;
    _starView.hasAnimation = YES;
    _starView.scorePercent = 0;
    [self.contentView addSubview:_starView];
    [_scoredLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.starView.mas_right).offset(10);
        make.top.equalTo(weakSelf.contentView).offset(0);
        make.width.mas_offset(50);
        make.bottom.mas_offset(-10);
    }];
    
}

- (void)starView:(HSStarView *)starRateView scroePercentDidChange:(CGFloat)newScorePercent{
    CGFloat score = 0.0;
    if (newScorePercent == 0) {
        score = 0.0;
    }else if (newScorePercent > 0.0 && newScorePercent <= 0.1) {
        score = 0.1;
    }else if (newScorePercent > 0.1 && newScorePercent <= 0.2) {
        score = 0.2;
    }else if (newScorePercent > 0.2 && newScorePercent <= 0.3) {
        score = 0.3;
    }else if (newScorePercent > 0.3 && newScorePercent <= 0.4) {
        score = 0.4;
    }else if (newScorePercent > 0.4 && newScorePercent <= 0.5) {
        score = 0.5;
    }else if (newScorePercent > 0.5 && newScorePercent <= 0.6) {
        score = 0.6;
    }else if (newScorePercent > 0.6 && newScorePercent <= 0.7) {
        score = 0.7;
    }else if (newScorePercent > 0.7 && newScorePercent <= 0.8) {
        score = 0.8;
    }else if (newScorePercent > 0.8 && newScorePercent <= 0.9) {
        score = 0.9;
    }else if (newScorePercent > 0.9 && newScorePercent <= 1.0) {
        score = 1.0;
    }
    CGFloat showScore = score * 5.0;
    self.starView.scorePercent = score;
    self.scoredLabel.text = [NSString stringWithFormat:@"%.1f分",showScore];
    NSLog(@"%.1lf",score);
    if (self.scoreBlock) {
        self.scoreBlock(self.indexPath, self.scoredLabel.text?:@"0分");
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
